<?php
function regnewsletter($email,$name)
{
    global $dbsql,$root_domain,$titlesite;
    
    ImportClass('account');
    
    if(isValidEmail($email)==false)
    {
        return array('err',__('Please enter a valid email address').'!');
    }
    //check email exists:
    $sql_select = "select cEmail from tblnewsletter_email where cEmail='$email'";
    $rs         = $dbsql->query($sql_select);
    if($dbsql->num_rows($rs)==0)
	{
		$activatecode     = gpc_Md5(time().$email).time();
        $datepost         = date("Y-m-d");

		$sql_insert = "INSERT INTO tblnewsletter_email
            		(cEmail,cName,cCode,iStatus,iMonth,dRegDate)
            		 VALUES
            		('".$email."','$name','".$activatecode."',0,0,'$datepost')";

		$dbsql->query($sql_insert);

		$linksubscribe     = $root_domain."/as/subscribe/".$activatecode.".htm";
		$linkunsubscribe   = $root_domain."/as/unsubscribe/".$activatecode.".htm";
        
        $linksubscribe      = '<a href="'.$linksubscribe.'">'.$linksubscribe.'</a>';
        $linkunsubscribe    = '<a href="'.$linkunsubscribe.'">'.$linkunsubscribe.'</a>';
        
        $emailconfig       = @getConfigEmail('RegisterNewletter');
    
        if($emailconfig==false)
        {
            //return array('config','Email not config');
        }else
        {
            $subject          = $emailconfig->title;
            $subject          = str_ireplace('{SITE}',$titlesite,$subject);
            
            $message          = str_ireplace('{EMAIL}',$email,$emailconfig->content);
            $message          = str_ireplace('{SITE}',$titlesite,$message);
            $message          = str_ireplace('{NAME}',$name,$message);
            $message          = str_ireplace('{LINKSUBSCRIBE}',$linksubscribe,$message);
            $message          = str_ireplace('{LINKUNSUBSCRIBE}',$linkunsubscribe,$message);
            
    		send_email($email,"",$subject,$message);
        }
		return array('ok',getLang("Thank you for signing up to our newsletter").'!');
	}
	else
	{
        return array('f',getLang("Email already exists").'!');
	}    
}

function subscribe()
{
    global $dbsql,$language,$code;

    $sql_select="select cEmail from tblnewsletter_email where cCode='$code'";
    $rs=$dbsql->query($sql_select);
    if($result=$dbsql->fetch_array($rs))
    {
    	$sql_update = "update tblnewsletter_email set iStatus='1' where cEmail='".$result['cEmail']."'";
    	$dbsql->query($sql_update);
    	returnMsg(__('Your email Activated'));
    }else
    {
    	returnMsg(__('Your email not exists in our system'));
    }
}

function unsubscribe()
{
    global $dbsql,$language,$code;
       
    $sql_select="select cEmail from tblnewsletter_email where cCode='$code'";
    $rs=$dbsql->query($sql_select);
    if($result=$dbsql->fetch_array($rs))
    {    	
    	$sql_delete = "delete from tblnewsletter_email where cEmail='".$result['cEmail']."'";
    	$dbsql->query($sql_delete);
    	
    	returnMsg(__('Your email Deleted'));
    }else
    {
    	returnMsg(__('Your email Deleted'));
    }
}
function returnMsg($msg)
{
    $html   = '<html><head><title>'.$msg.'</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
    $html  .= '<meta http-equiv="REFRESH" content="10; url=/">';
    $msg   .= '<br><a href="/">'.__('go to home page').'</a>';
    $html  .= '<style>.msg{width:60%;padding:25px;text-align:center;color:#ff0000;border: 1px solid #ff0000}</style>';
    $html  .= '</head><body><div align=center><div class="msg">'.$msg.'</div></div></body></html>';
    exit($html);
}

function ListContentNewsletter($idP)
{
    global $dbsql,$language;
    
    $sql_select = "select * from tblnewsletter_content where iParentId='$idP' and iStatus=1 order by iOrder";
    return $dbsql->load($sql_select);
}

function BuildHTMLNewsLetter($newsletter,$isCron=false)
{
    global $root_domain,$folder_path_root,$folder_img_upload;
    
    $linkroot  = $root_domain.$folder_path_root;
    $linkimgup = $linkroot.$folder_img_upload;
    
    $NameGuest = 'Guest';
    $footermsg = '<DIV style="padding-top:10px">Happy travels,<br><b>The Team at Grandeur Journeys</b></DIV>';
    $contents  = ListContentNewsletter($newsletter['iId']);;
    $linkunsubcribe= '{LINKUNSUBCRIBE}';
    
    $linkthis= $linkroot.getUrlCatByType('newsletter',false).'/'.$newsletter['cUrlString'].'/';//link to this
    $html      = '';
    if($isCron)
    {
        $html .='<style>
                    .intro a{color:#025F36;text-decoration: none;}
                    .intro a:hover{color:#4C483F}
                    .menubottom a{color:#fff;text-decoration: none;}
                    .menubottom a:hover{color:#025F36}
                </style>';
        $html.= '<base href="'.$root_domain.'" />';
        $html.= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
        $html.= '<body style="font-size: 12px; font-family: Verdana,sans-serif;color:#232323 ;">';
    }
    $html.= '<div align="center">';
    $html.= '<table width="660" cellpadding="0" cellspacing="0" style="background: #fff;">';    
  
    if($isCron)
    {
        $html.= '<tr><td align="right">
                    <a href="'.$linkthis.'" style="color: #000;text-decoration: none;">
                        '.('Having trouble viewing this e-mail? View it in your browser').'
                    </a>
                    </td>
                </tr>';
    }
    $html.='<tr><td style="border: 1px solid #5D471F;width:660px;" align="center">';
    $html.='<table width="100%"  style="background: #5D471F;color:#fff;height:55px" cellpadding="0" cellspacing="0">';
    $html.='<tr>
                <td width="450" style="font-size: 15px;padding:8px 0px 5px 15px">     
                    <div>'.('GRANDEUR journeys').'</div>
                    <div style="color:#fff;font-size:11px;padding-top:5px">
                        '.('Extraordinary Experiences of Indochina & beyond').'
                    </div>
                </td>
                <td style="padding-right:10px" align="right">
                    <a href="'.$root_domain.'">
                        <img src="'.$linkroot.'images/logow.gif" width="130" border="0" />
                    </a>  
                </td>
            </tr>';
                 
    $imglink    = $linkimgup.$newsletter['cImage'];
    if($imglink)
    {
        $html.='
                <tr>
                    <td colspan="2">
                        <img src="'.$imglink.'" width="660" style="display: block;" />
                    </td>
                </tr>';
     }
     $html.='</table><br />';
     
     $html.= '<table cellpadding="0" cellspacing="0" width="97%" style="border:1px solid #dddddd;border-bottom:0px">            
                <tr style="background: #ffffff;">
                    <td style="font-size: 15px;font-weight:600;color:#025F36;padding:10px;height: 25px">
                        '.$newsletter['cName'].'
                    </td>
                    <td width="5%" style="padding-right: 10px;">
                        <a href="'.$linkthis.'sendfriend">
                            <img src="'.$linkroot.'images/btn_sendtofriend.gif" width="136" height="21" border="0" /> 
                        </a>
                    </td>
                </tr>
                <tr style="background:#EAEAEA">
                    <td colspan="2" style="padding: 20px;font-size:14px">
                        <div>'.('Dear').' '.$NameGuest.'!</div>               
                        <div align="left">                    
                            '.$newsletter['cDescription'].'
                            '.(($isCron)? $footermsg : '').'
                        </div>
                    </td>
                </tr>
           </table>';
           
           
    $html.='<table  cellpadding="0" cellspacing="0" width="97%" style="border-left:1px solid #dddddd;border-right:1px solid #dddddd">';
      
    foreach($contents as $i => $content)
    {
        $imglink    = $linkimgup.$content['cImage'];
        
        $html.='
                <tr>
                    <td colspan="2">&nbsp;</td>
                </tr>          
                <tr>
                    <td width="160" align="right" valign="top" style="padding-left: 20px;">
                        <div style="border: 1px solid #cccccc;padding:2px">
                            <img src="'.$imglink.'" width="160"/>
                         </div>                        
                    </td>
                    <td valign="top">
                        <div style="margin-left:15px;margin-right:15px">
                            <div style="font-size: 15px;font-weight:600;color:#025F36;">'.$content['cName'].'</div>
                            <div class="intro" style="color: #343434;font-size: 12px; font-family: Verdana,sans-serif;line-height:18px">
                                '.$content['cIntro'].'</div>
                        </div>
                    </td>
                </tr>';
    }
    $html.='<tr>                
                <td colspan="2" align="center">
                    '.$newsletter['cFooter'].'
                </td>
            </tr>
        </table>';
    $html.='<table  cellpadding="0" cellspacing="0" width="97%" style="border-left:1px solid #dddddd;border-right:1px solid #dddddd;background: #5D471F;">';
    $html.='<tr>
                <td style="padding-right:5px;padding-left:10px" height="37">
                    <img src="'.$linkroot.'images/logofooter.png" />                    
                </td>
                <td style="color:#fff;font-size:11px;">
                    '.getConfig('FooterPage').'
                </td>
                <td class="menubottom" style="font-size:11px;color: #fff;padding-right:10px" align="right">
                    <a target="_blank" style="color: #fff;text-decoration: none;font-size:11px" href="'.$linkroot.'about-us">About GRANDEUR journeys</a> | 
                    <a target="_blank" style="color: #fff;text-decoration: none;font-size:11px" href="'.$linkroot.'privacy-statement">Privacy Policy</a>
                </td>
            </tr>
        </table>';
    $html.='<br /></td></tr></table>';
    if($isCron)
    {
        $html.='<div style="padding:20px" align="center">';
        $html.='<a href="'.$linkunsubcribe.'" style="color: #000;text-decoration: none;font-size:11px">';
        $html.=('Unsubcribe');
        $html.='</a></div></div></body>';
    }
    else
    {
        $html.='</div>';
    }
    
    
    return $html;
}
?>